/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 * Description: cann error status reporter
 * Author: huawei
 * Create: 2024-10-14
 */

#ifndef RAS_REPORTER_H
#define RAS_REPORTER_H

#include <map>
#include "adapter_hal.h"

namespace dfx {

enum class ReportStatus : int64_t {
    kDefault = 0,
    kRetrySuccess,
    kRetryWithBackupLink,
    kRetryFail,
};

class CannErrorReporter {
public:
    // 单例
    static CannErrorReporter& GetInstance();

    // 解注册所有保存的 Sensor Node，并清空用于缓存 Sensor Node 句柄的 map
    HcclResult Clear();

    // 向 Sensor Node 报告故障状态更新
    HcclResult UpdateSensorNode(uint32_t deviceId, ReportStatus reportStatus);

private:
    CannErrorReporter();
    ~CannErrorReporter();

    // 初始化并注册 Sensor Node
    HcclResult Init(uint32_t deviceId);

    // 构造 Sensor Node 注册参数
    HcclResult ConstructSensorNodeCfg(halSensorNodeCfg &cfg);

    // 注册新的 Sensor Node，并返回该 Sensor 的句柄
    HcclResult RegisterSensorNode(uint32_t deviceId, uint64_t *handle);

    // 解注册指定的 Sensor Node
    HcclResult UnRegisterSensorNode(uint32_t deviceId, uint64_t handle);

    // Sensor Node 的句柄
    uint64_t sensorHandle_;
    uint32_t devId_;

    // 故障状态对应的上报事件类型
    std::map<ReportStatus, std::pair<HcclReportEvent, halGeneralEventType_t>>
        status2Event_ =
    {
        {
            ReportStatus::kRetrySuccess,
            { HcclReportEvent::HCCL_OP_RETRY_SUCCESS, GENERAL_EVENT_TYPE_ONE_TIME }
        },
        {
            ReportStatus::kRetryWithBackupLink,
            { HcclReportEvent::HCCL_OP_USE_BACKUP_LINK, GENERAL_EVENT_TYPE_ONE_TIME }
        },
        {
            ReportStatus::kRetryFail,
            { HcclReportEvent::HCCL_OP_RETRY_FAIL, GENERAL_EVENT_TYPE_ONE_TIME }
        },
    };
};

} // namespace
#endif // RAS_REPORTER_H